var gameStatus = "pindding";

// 点击道具按钮
function doFlicker(btnItem, name){
  if(gameStatus == "pindding"){
    // 游戏开始
    startTime();
  }else if(gameStatus != "started"){
    // 游戏已结束
    return false;
  }

  $clickBtn = $(btnItem);
  if($clickBtn.hasClass("enabled")){
    // 按钮上次点击后还没有恢复到可点击
    return false;
  }

  // 清除现在有bug_on class(闪动的元素)
  $("li div.bug_on").removeClass("bug_on");

  var selectorName = "div.bug img.none";
  switch(name){
  case "fire":
    selectorName = "div.bug img.c_1, div.bug img.c_2, div.bug img.c_3";
    break;
  case "net":
    selectorName = "div.bug img.c_1, div.bug img.c_2, div.bug img.c_3,div.bug img.b_1, div.bug img.b_2";
    break;
  case "cleaner":
    selectorName = "div.bug img";
    break;
  }
  var activeClassName = name+"_on";
  $(selectorName).each(function(){
    $parentDiv = $(this).parents("div.bug");
    $parentDiv.addClass("bug_on");
  });
  // 记录当前被点击的按钮名
  activeBtnName = name;
  return true;
}

// 移除道具按钮的enabled Class使按钮可点
function removeBtnEnabledClass(btnSelector){
  $(btnSelector).removeClass("enabled");
}

// 图片淡入完后移出hide class和fade_in class，使该img在下次使用道具时可消除
function removeImageFadeIn(hideClassName){
  $("img."+hideClassName).removeClass(hideClassName).removeClass("fade_in");
}

function imageAnimation(data){
  // console.log(data);
  className = data["class_name"];
  activeClassName = data["class_name"] + "_on";
  hideClassName = data["class_name"] + "_hide";
  $cleanImgs = $("img."+hideClassName);
  $parentBugDivs = $cleanImgs.parents("div.bug");
  $parentBugDivs.removeClass("bug_on");

  //  闪动效果1s动画
  $parentBugDivs.siblings("div."+className).addClass(activeClassName);
  $parentBugDivs.siblings("div.light").addClass("light_on");
  setTimeout(function(){
    // 去看闪动效果动画
    $parentBugDivs.siblings("div."+activeClassName).removeClass(activeClassName);
    $parentBugDivs.siblings("div.light_on").removeClass("light_on");
  }, 1000);


  var idx = 0;
  var arrClassNames = data["items"];
  $cleanImgs.each(function(){
    $img = $(this);
    var imgClassName = arrClassNames[idx++];
    $img.addClass(imgClassName).attr("src", "images/"+imgClassName+".png");
    $bugDiv = $("img").parents("div.bug");

    $img.parents("li").removeClass("disable");
  });
  $cleanImgs.addClass("fade_in");
  setTimeout(function(){
    $cleanImgs.removeClass("fade_in").removeClass(hideClassName);
  }, 1000);
  $divScore = $("div#game_score");
  current_score = parseInt($divScore.html());
  $divScore.html(current_score+data["scores"]);
}

// 消除img
function doCleanUp(clickItem){
  if(gameStatus != "started"){
    // 游戏已结束
    return false;
  }

  btnNameSelector = "div.game_btn_"+activeBtnName;
  $btnItem = $(btnNameSelector);
  $clickItem = $(clickItem);
  canClean = false;
  if(!$btnItem.hasClass("enabled") && $clickItem.hasClass("bug_on")){
    canClean = true;
  }
  if(canClean){
    secs = 10000;
    switch(activeBtnName){
    case "fire":
      secs = 2000;
      break;
    case "net":
      secs = 6000;
      break;
    case "cleaner":
      secs = 40000;
      break;
    }
    
    // 按钮变成不可点
    $btnItem.addClass("enabled");
    $btnItem.find(".on").css('width','0px');
    $btnItem.find(".on").animate({width:"207px"}, secs);
    setTimeout("removeBtnEnabledClass('"+btnNameSelector+"')", secs);

    $cleanImg = $("li div.bug_on img");
    var patt1 = new RegExp(/[abc]_\d/);
    data = {class_name: activeBtnName};
    $cleanImg.each(function(){
      imgClassName = $(this).attr("class");
      res = imgClassName.match(patt1);
      if(res){
        name = res[0];
        if(data[name]){
          data[name] = data[name] + 1;
        }else{
          data[name] = 1;
        }
        $(this).removeClass(name).addClass(activeBtnName+"_hide");  
      }
    });

    
    switch(activeBtnName){
    case "fire":
      json_data = {"scores": 1500, "class_name": "fire", "items": ["c_1",
        "c_3", "b_1", "b_2", "c_2", "c_2", "b_1", "c_1",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "c_2", "b_2", "b_1", "c_2", "c_2", "b_2", "c_3"]};
      break;
    case "net":
      json_data = {"scores": 3000, "class_name": "net", "items": ["b_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_2", "c_2", "b_1", "c_2", "c_2", "c_2", "c_1"]};
      break;
    default:
      json_data = {"scores": 3000, "class_name": "", "items": ["c_2",
        "b_1", "c_2", "c_3", "b_2", "b_1", "c_1", "c_3",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_1", "c_2", "c_1", "c_2", "b_1", "c_1", "c_2",
        "b_2", "c_3", "c_1", "c_2", "b_2", "c_2", "c_1"]};
      break;
    }
    setTimeout(imageAnimation(json_data), 4000);

    // $.ajax({
    //  type:"GET",
    //  url: GV.DOMAIN+"Waterworld/Index/save_game",
    //  dataType: 'JSON',
    //  data: data,
    //  success: function(json_data){
    //   setTimeout(imageAnimation(json_data), 4000);  
    //  },error: function(){
    //     //showSlotResult(runResultData);
    //  }
    // });
  }
}

// 游戏计时器
function startTime(){
  window.timer=setInterval(function(){
    $time = $("div#game_timer");
    leftSeconds = parseInt($time.html());
    if(leftSeconds > 0){
      if(gameStatus != "started"){
        gameStatus = "started";
      }
      leftSeconds--;
      $time.html(leftSeconds);
    }else{
      clearInterval(window.timer);
      gameStatus = "stoped";
      $div = $("div#game_over");
      $div.css({'display':'block'});
    }
  }, 1000);
  }